package cn.bjc.user.service.impl;

import cn.bjc.basic.constant.RegisterConstant;
import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.service.impl.BaseServiceImpl;
import cn.bjc.basic.util.Md5Utils;
import cn.bjc.basic.util.StrUtils;
import cn.bjc.user.domain.Logininfo;
import cn.bjc.user.domain.User;
import cn.bjc.user.dto.EmailRegisterDto;
import cn.bjc.user.dto.RegisterDto;
import cn.bjc.user.mapper.LogininfoMapper;
import cn.bjc.user.mapper.UserMapper;
import cn.bjc.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
//父类有事务,该注解有继承性
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    
    @Override
    public void phoneRegister(RegisterDto dto) {

        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();

        //1.校验  非空校验
        if(StringUtils.isEmpty(phone)||
           StringUtils.isEmpty(verifyCode)||
           StringUtils.isEmpty(password)||
           StringUtils.isEmpty(comfirmPassword)
        ){
            throw new BusinessException("参数不能为空");
        }
        
        //2.两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinessException("两次密码输入不一致，请重新输入！");
        }
        
        //3.校验  手机号是否被注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("该手机号已经注册,请直接登陆!");
        }
        
        //4.校验 验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if(obj==null){
            //验证码过期了
            throw new BusinessException("验证码过期,请重新获取!");
        }
        
        //5.校验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if (!verifyCode.equalsIgnoreCase(redisCode)) {
            throw new BusinessException("验证码错误,请重新输入!");
        }
        
        //6.保存数据: 先存表1:logininfo   后存表2:user
        //先去获取logininfo对象和user对象
        Logininfo logininfo = dto2Logininfo(dto);
        //通过logininfo对象获取user对象
        User user = logininfo2User(logininfo);

        //先存表1:logininfo,返回自增长主键
        logininfoMapper.save(logininfo);
        
        
        //把自增长主键赋值给user对象,再保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User logininfo2User(Logininfo logininfo) {
        
        User user = new User();
        
        //复制logininfo和User共同拥有的属性
        BeanUtils.copyProperties(logininfo, user);
        
        return user;
    }

    //通过dto创建logininfo对象
    public Logininfo dto2Logininfo(RegisterDto dto){
        Logininfo logininfo = new Logininfo();
        /*
            //用户姓名
            private String username;
            //电话
            private String phone;
            //邮箱
            private String email;
            //盐值
            private String salt;
            //密码
            private String password;
            //类型:   0管理员   1普通用户
            private Integer type = 1;
         */
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        
        //生成盐值  32位
        String salt = StrUtils.getComplexRandomString(32);
        
        //通过盐值和密码加密     此处为加盐加密!!
        String dbpassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        
        //生成的盐值和密码赋值给对象
        logininfo.setSalt(salt);
        logininfo.setPassword(dbpassword);
        
        return logininfo;
    }
    
    

    @Override
    public void emailRegister(EmailRegisterDto dto) {

        String email = dto.getEmail();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();

        //1.校验  非空校验
        if(StringUtils.isEmpty(email)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(comfirmPassword)
        ){
            throw new BusinessException("参数不能为空");
        }
        
        //效验邮箱合法性
        
        
        //2.两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinessException("两次密码输入不一致，请重新输入！");
        }

        //3.校验  手机号是否被注册
        User userEmail = userMapper.findByEmail(email);
        if(userEmail!=null){
            throw new BusinessException("该邮箱已经注册,请直接登陆!");
        }
        //4.生成盐值  32位
        String salt = StrUtils.getComplexRandomString(32);
        //通过盐值和密码加密     此处为加盐加密!!
        String dbpassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        
        //4.保存数据:
        User user = new User();

        user.setEmail(dto.getEmail());
        user.setSalt(salt);
        user.setPassword(dbpassword);
        userMapper.save(user);
    }
    
}
